﻿@{
    Layout = "~/Views/Shared/_LayoutUser.cshtml";
    ViewBag.Title = "网非e学院--注册";
}
<form method="post" id="formRegister">
    <div class="rg_Register">
        <div class="rg_Line"></div>
        <div class="rg_TextList">
            <div class="rg_Yaoqingren">
                <img class="rg_YaoqingrenImg" src="@ViewBag.InviterImg" />&nbsp;@ViewBag.InviterName
            </div>
            <div class="rg_Div">
                <input class="rg_Text" type="text" placeholder="邀请人信息" readonly="readonly" />
            </div>
            <div class="rg_Div">
                <input class="rg_Text" type="text" placeholder="手机号码" pattern="[0-9]*" value="@ViewBag.Phone" id="phone" name="Phone" />
            </div>
            <div class="rg_Div">
                <input class="rg_Text" type="text" placeholder="请输入验证码" id="mobileCode" name="MobileCode" />
                <span class="rg_ValiBtn" onclick="SendCode()" id="btnSend">获取验证码</span>
            </div>
            <div class="rg_Div">  </div>
            <input class="rg_Text" type="password" placeholder="密码设置（8-16位的数字、字母、符号）" id="pwd1" name="Pwd" />
            <div class="rg_Div">
                <input class="rg_Text" type="password" placeholder="重复输入密码" id="pwd2" />
            </div><br />
            <div id="ErrorInfo" class="error">
                @ViewBag.ErrorInfo
            </div>
            <span class="customBtn" onclick="Register()">注册</span>
        </div>
    </div>
    <div>
        <input name="InviterID" type="hidden" value="@ViewBag.InviterID" />
        <input name="InviterPhone" type="hidden" value="@ViewBag.InviterPhone" />
        <input name="InviterName" type="hidden" value="@ViewBag.InviterName" />
        <input name="InviterImg" type="hidden" value="@ViewBag.InviterImg" />

        <input type="hidden" name="Code" value=" @ViewBag.Code" />
        <input type="hidden" name="State" value=" @ViewBag.State" />
        <input type="hidden" name="Token" value=" @ViewBag.Token" />
        <input type="hidden" name="Image" value=" @ViewBag.Image" />
        <input type="hidden" name="NickName" value=" @ViewBag.NickName" />
        <input type="hidden" name="OpenID" value=" @ViewBag.OpenID" />
        <input type="hidden" name="Sex" value=" @ViewBag.Sex" />
    </div>

</form>
<script src="~/Scripts/PhoneStyle.js"></script>
<script>
    var curCount = 0;
    //发送短信验证码
    function SendCode(phone) {
        var phone = $("#phone").val().replace(/\s/g, "");
        if (phone.length != 11) {
            $("#ErrorInfo").html("手机号输入错误！");
            return -1;
        }
        if (isNaN(phone)) {
            $("#ErrorInfo").html("手机号输入错误！");
            return -2;
        }

        curCount = 100;
        $("#btnSend").removeAttr("onclick");
        $("#btnSend").addClass("rg_ValiBtnSel");
        $("#btnSend").html(curCount);

        $.ajax({
            type: "Post", //提交方式
            url: "SendValiCode",//获取数据的函数
            data: { "phone": phone }, //参数
            error: function () { $("#ErrorInfo").html("验证码发送失败，请重试！"); },
            success: function (data) {
                if (data == "1") {
                    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
                }
            }
        });
    }

    //倒计时
    function SetRemainTime() {
        if (curCount <= 0) {
            window.clearInterval(InterValObj);//停止计时器
            $("#btnSend").attr("onclick", "SendCode()");
            $("#btnSend").html("发送");
            $("#btnSend").removeClass("rg_ValiBtnSel ");
        }
        else {
            curCount--;
            $("#btnSend").html(curCount);
        }
    }


    //手机号文本框变更时执行
    $("#phone").keyup(function () {
        PhoneStyle("#phone");
        ValidatePhoneNum();
    })

    //清除提醒文字
    function ClearError() {
        $("#ErrorInfo").html("");
    }

    //验证手机号是否数字
    function ValidatePhoneNum() {
        ClearError();
        var num = $("#phone").val().replace(/\s/g, "");
        if (isNaN(num)) {
            $("#ErrorInfo").html("手机号输入错误！");
            return -1;
        }
        return 0;
    }

    //验证手机号位数
    function ValidatePhone() {
        var num = $("#phone").val().replace(/\s/g, "");
        if (num.length != 11) {
            $("#ErrorInfo").html("手机号输入错误！");
            return -1;
        }
        return 0;
    }

    //验证短信
    function ValidataMobileCode() {
        ClearError();
        var num = $("#phone").val().replace(/\s/g, "");
        var code = $("#mobileCode").val().replace(/\s/g, "");
        if (code == "") {
            $("#ErrorInfo").html("请输入验证码！");
            return -1;
        }

        $.ajax({
            type: "Post", //提交方式
            url: "GetValiCode",//获取数据的函数
            data: { "phone": num }, //参数
            error: function () { $("#ErrorInfo").html("验证码错误！"); return -2; },
            success: function (data) {
                if (data == code) {
                    //密码 MD5加密
                    $("#pwd1").val($.md5($("#pwd1").val()));
                    //提交表单 注册
                    $("#formRegister").submit();
                }
                else {
                    $("#ErrorInfo").html("验证码错误！");
                    return;
                }
            }
        });
    }

    //验证密码长度
    function ValidatePwd1() {
        ClearError();
        var pwd1 = $("#pwd1").val().trim();
        if (pwd1.length < 8 || pwd1.length > 16) {
            $("#ErrorInfo").html("请输入8-16位密码！");
            $("#pwd1").val("");
            $("#pwd2").val("");
            return -1;
        }
        return 0;
    }

    //验证两次输入密码是否一致
    function ValidatePwd2() {
        ClearError();
        var pwd1 = $("#pwd1").val().trim();
        var pwd2 = $("#pwd2").val().trim();
        if (pwd1 != pwd2) {
            $("#ErrorInfo").html("两次密码输入不一致！");
            $("#pwd1").val("");
            $("#pwd2").val("");
            return -1;
        }
        return 0;
    }

    //用户注册
    function Register() {
        ClearError();
        var res = 0;

        //验证手机号
        res = ValidatePhoneNum();
        if (res < 0) {
            return;
        }

        res = ValidatePhone();
        if (res < 0) {
            return;
        }

        res = ValidatePwd1()
        if (res < 0) {
            return;
        }

        res = ValidatePwd2()
        if (res < 0) {
            return;
        }

        //短信验证码
        res = ValidataMobileCode();

       
    }
</script>
